package com.his.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.his.common.Const;
import com.his.common.Rsp;
import com.his.entity.Patient;
import com.his.entity.Record;
import com.his.mapper.RecordMapper;
import com.his.service.RecordService;
import com.his.utils.JWTUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Record)表服务实现类
 *
 * @author makejava
 * @since 2020-10-23 18:16:13
 */
@Service("recordService")
public class RecordServiceImpl implements RecordService {
    @Resource
    private RecordMapper recordMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Record queryById(Integer id) {
        return this.recordMapper.queryById(id);
    }

    /**
     * 查询所有通过实体类属性
     *
     * @param record 实例对象
     * @return 查询的结果
     */
    @Override
    public List<Record> queryAll(Record record) {
        return this.recordMapper.queryAll(record);
    }

    /**
     * 新增数据
     *
     * @param record 实例对象
     * @return 实例对象
     */
    @Override
    public Record insert(Record record) {
        this.recordMapper.insert(record);
        return record;
    }

    /**
     * 修改数据
     *
     * @param record 实例对象
     * @return 实例对象
     */
    @Override
    public Record update(Record record) {
        this.recordMapper.update(record);
        return this.queryById(record.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.recordMapper.deleteById(id) > 0;
    }

    @Override
    public Record selectOneByPatientId(String patientId, String token) throws Exception {
        if (!StringUtils.isEmpty(patientId) && StringUtils.isNumber(patientId)) {
            return recordMapper.queryByPatientId(Integer.parseInt(patientId));
        }
        //解析token判断是否是病人
        if (!StringUtils.isEmpty(token)) {
            Object o = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
            if (o instanceof Patient) {
                Patient p = (Patient) o;
                return recordMapper.queryByPatientId(p.getId());
            }
        }
        return null;
    }

    @Override
    public Rsp insertOne (Record record,String token) throws Exception{
        //解析token判断是否是病人
        Object o = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        if (o instanceof Patient) {
            Patient p = (Patient) o;
            //去数据库查找该用户有没有生成病例id
            if (recordMapper.queryByPatientId(p.getId()) != null) {
                //已经有病历了
                return Rsp.fail().msg("已经填写过病历了不要重复填写");
            }
            record.setPatientId(p.getId());
        }
        return Rsp.success(recordMapper.insert(record));
    }

    @Override
    public Rsp updateOne (Record record){
        int update = this.recordMapper.update(record);
        if (update > 0) {
            return Rsp.success(record).msg("修改成功");
        } else {
            return Rsp.fail();
        }
    }
}
